package com.example.canvastest;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.util.Log;

public class AccelerometerService extends Service implements SensorEventListener {
	public static volatile float accel;
	public static volatile float accelY;
	SensorManager mSensorManager;
	
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		Log.d("test", "onCreate");
		
		mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		
		// Register for events.
		mSensorManager.registerListener(
			  this,
			  mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION),
			  SensorManager.SENSOR_DELAY_GAME);
	}

	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSensorChanged(SensorEvent arg0) {
		Log.d("test", "onSensorChanged: " + accel);
		accel = arg0.values[0];
		accelY = arg0.values[1];
	}
	
	@Override
	public void onDestroy() {
		Log.d("onDestroy", "unregisterListener");
		mSensorManager.unregisterListener(this);
	}
}
